<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="路线名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入路线名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出行区域" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择区域">
          <el-option key="1" label="省内" value="省内"></el-option>
          <el-option key="2" label="省外" value="省外"></el-option>
          <el-option key="3" label="国外" value="国外"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="携带家属" prop="canBring">
        <el-select v-model="queryParams.canBring" placeholder="携带家属">
          <el-option key="1" label="允许" value="允许"></el-option>
          <el-option key="2" label="不允许" value="不允许"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="路线状态">
          <el-option key="1" label="未提交" value="未提交"></el-option>
          <el-option key="2" label="待审核" value="待审核"></el-option>
          <el-option key="3" label="未通过" value="未通过"></el-option>
          <el-option key="4" label="已通过" value="已通过"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['lxy:route:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleSelectBefore"
          v-hasPermi="['lxy:route:add']">选择往年路线</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleSubmit"
          v-hasPermi="['lxy:route:edit']">提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lxy:route:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['lxy:route:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="路线名" align="center" prop="name" />
      <el-table-column label="出行区域" align="center" prop="area" />
      <el-table-column label="预算" align="center" prop="budget" />
      <el-table-column label="允许携带家属" align="center" prop="canBring" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>
            {{ scope.row.status }}
            <!-- 为 '未通过' 状态添加提示框 -->
            <el-tooltip v-if="scope.row.status === '未通过'" effect="light" placement="top">
              <div slot="content">未通过原因：{{ scope.row.noPassReason }}</div>
              <i class="el-icon-question"></i>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="照片集" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lxy:route:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSubmit(scope.row)"
            v-hasPermi="['lxy:route:edit']">提交</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lxy:route:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="路线名" prop="name">
          <el-input v-model="form.name" placeholder="请输入路线名" />
        </el-form-item>
        <el-form-item label="预算" prop="budget">
          <el-input v-model="form.budget" placeholder="请输入预算" />
        </el-form-item>
        <el-form-item label="是否允许携带家属" prop="canBring">
          <el-radio-group v-model="form.canBring">
            <el-radio label="允许">允许</el-radio>
            <el-radio label="不允许">不允许</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="选择出行区域" prop="area">
          <el-radio-group v-model="form.area">
            <el-radio label="省内">省内</el-radio>
            <el-radio label="省外">省外</el-radio>
            <el-radio label="国外">国外</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="注意事项" prop="notice">
          <el-input v-model="form.notice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="路线介绍">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="照片集" prop="pic">
          <image-upload v-model="form.pic" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择过去路线的对话框 -->
    <el-dialog title="往年路线" :visible.sync="before.dialogVisible" width="80%" :before-close="handleBeforeClose">
      <el-table :data="before.routeList" @selection-change="handleSelectionChangeBefore">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="路线名" align="center" prop="name" />
        <el-table-column label="出行区域" align="center" prop="area" />
        <el-table-column label="状态" align="center" prop="status" />
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form>
              <el-form-item label="路线名">
                <span>{{ scope.row.name }}</span>
              </el-form-item>
              <el-form-item label="区域">
                <span>{{ scope.row.area }}</span>
              </el-form-item>
            </el-form>
            <el-form label-position="left" inline>
              <el-form-item label="预算">
                <span>{{ scope.row.budget }}</span>
              </el-form-item>
            </el-form>
            <el-form label-position="left" inline>
              <el-form-item label="允许携带家属">
                <span>{{ scope.row.canBring }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="before.total > 0" :total="before.total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getBeforeList" />

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleBeforeClose">取 消</el-button>
        <el-button type="primary" @click="submitBeforeForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listRoute, getRoute, delRoute, addRoute, updateRoute, subRoute, listRouteBeforeByAgency,addBeforeRoute } from "@/api/lxy/route";


export default {
  name: "Route",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 路线表格数据
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        area: undefined,
        departmentNo: undefined,
        status: undefined,
      },
      before: {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 路线表格数据
        routeList: [],
        dialogVisible: false,
        hasList: false
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "路线名不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        notice: [
          { required: false, message: "注意事项", trigger: "blur" }
        ],
        url: [
          { required: false, message: "连接地址", trigger: "blur" }
        ],
        area: [
          { required: true, message: "0省内,1省外,2国外不能为空", trigger: "blur" }
        ],
        budget: [
          { required: false, message: "预算", trigger: "blur" }
        ],
        canBring: [
          { required: true, message: "是否允许携带家属不能为空", trigger: "blur" }
        ],
        pic: [
          { required: false, message: "路线照片", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询路线列表 */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then(response => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        projectId: undefined,
        travelagenId: undefined,
        name: undefined,
        content: undefined,
        notice: undefined,
        url: undefined,
        area: undefined,
        budget: undefined,
        status: undefined,
        pic: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加路线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getRoute(id).then(response => {
        this.loading = false;
        this.form = response.data.agency;
        this.travelagenOptions = response.data.travelagen;
        this.open = true;
        this.title = "修改路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRoute(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRoute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除路线名为"' + names + '"的数据项？').then(() => {
        this.loading = true;
        return delRoute(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleSubmit(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认提交路线名为"' + names + '"的数据项？').then(() => {
        this.loading = true;
        return subRoute(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lxy/route/export', {
        ...this.queryParams
      }, `route_${new Date().getTime()}.xlsx`)
    },



    /**以下针对选择往年路线 */
    /** 选择往年路线按钮操作 */
    handleSelectBefore() {
      if (!this.before.hasList) {
        this.getBeforeList();
        this.before.hasList = true;
      }
      this.before.dialogVisible = true;
    },
    /**获取往年路线 */
    getBeforeList() {
      this.before.loading = true;
      listRouteBeforeByAgency(this.queryParams).then(response => {
        this.before.routeList = response.rows;
        this.before.total = response.total;
        this.before.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChangeBefore(selection) {
      this.before.ids = selection.map(item => item.id)
      this.before.names = selection.map(item => item.name)
    },
    handleBeforeClose(){
      this.before.ids = [];
      this.before.names=[];
      this.before.dialogVisible = false
    },
    /**选择往年路线确定 按钮 */
    submitBeforeForm() {
      const ids =  this.before.ids;
      const names =  this.before.names;
      this.$modal.confirm('是否确认选择往年路线名为"' + names + '"为新的路线？').then(() => {
        this.before.loading = true;
        return addBeforeRoute(ids);
      }).then(() => {
        this.before.loading = false;
        this.before.dialogVisible = false;
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {
        this.$modal.msgSuccess("提交失败");
      }).finally(() => {
        this.before.loading = false;
      });
    },






  }
};
</script>
